<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户数据查询</title>
    <link href="/api/assets/css/bootstrap.min.css" rel="stylesheet">
    <link href="/api/assets/css/bootstrap-icons.css" rel="stylesheet">
    <style>
        .container {
            max-width: 800px;
            margin-top: 50px;
        }
        .card {
            margin-bottom: 20px;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
        }
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        .toast-container {
            position: fixed;
            top: 20px;
            right: 20px;
            z-index: 1050;
        }
        .toast {
            min-width: 250px;
        }
        .instructions {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            border-left: 4px solid #0d6efd;
        }
        .instructions ol {
            margin-bottom: 0;
            padding-left: 20px;
        }
        .instructions li {
            margin-bottom: 8px;
        }
        .instructions .important {
            color: #dc3545;
            font-weight: bold;
        }
        .float-button {
            position: fixed;
            bottom: 30px;
            right: 30px;
            z-index: 1000;
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background-color: #198754;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 4px 10px rgba(0,0,0,0.3);
            cursor: pointer;
            transition: all 0.3s;
        }
        .float-button:hover {
            transform: scale(1.1);
            box-shadow: 0 6px 15px rgba(0,0,0,0.3);
        }
        .float-button i {
            font-size: 24px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2 class="text-center mb-4">体验广告推送(代替扫码 后续流程一致)</h2>
        
        <!-- 使用说明 -->
        <div class="instructions">
            <h5>使用说明</h5>
            <ol>
                <li>下方选择APP，选择快手号或手机号，输入对应信息</li>
                <li>输入信息完成之后点击推送按钮</li>
                <li>打开对应的快手，打开"发现页"或"精选"页面，上下滑动，找到带有广告标识的</li>
                <li>从广告进入游戏，等待10分钟以上即可开始观看广告 <span class="important">(从广告进入游戏之后左上角设置里面登录)</span></li>
            </ol>
        </div>
        
        <div class="card">
            <div class="card-body">
                <form id="pushForm">
                    <div class="mb-3">
                        <label for="appSelect" class="form-label">选择应用</label>
                        <select class="form-select" id="appSelect" required>
                            <option value="" selected disabled>请选择应用</option>
                            <!-- 应用选项将通过后端API动态加载 -->
                        </select>
                        <div id="appNameDisplay" class="mt-2 text-muted small" style="word-wrap: break-word;"></div>
                    </div>
                    
                    <div class="mb-3">
                        <label for="searchType" class="form-label">查询方式</label>
                        <select class="form-select" id="searchType">
                            <option value="kuaishouId" selected>快手ID</option>
                            <option value="phoneNumber">手机号</option>
                        </select>
                    </div>
                    
                    <div class="mb-3" id="kuaishouIdField">
                        <label for="kuaishouId" class="form-label">快手ID</label>
                        <input type="text" class="form-control" id="kuaishouId" placeholder="请输入快手ID">
                    </div>
                    
                    <div class="mb-3" id="phoneNumberField" style="display: none;">
                        <label for="phoneNumber" class="form-label">手机号</label>
                        <input type="text" class="form-control" id="phoneNumber" placeholder="请输入手机号">
                    </div>
                    
                    <div class="d-grid">
                        <button type="submit" class="btn btn-primary">推送</button>
                    </div>
                </form>
            </div>
        </div>
        
        <div class="loading" id="loading">
            <div class="spinner-border text-primary" role="status">
                <span class="visually-hidden">Loading...</span>
            </div>
            <p class="mt-2">正在推送，请稍候...</p>
        </div>
    </div>
    
    <!-- 悬浮按钮 - 跳转到event-search页面 -->
    <div class="float-button" id="eventSearchButton" title="查看事件数据">
        <i class="bi bi-search"></i>
    </div>
    
    <!-- 弹窗提示 -->
    <div class="toast-container">
        <div id="resultToast" class="toast align-items-center" role="alert" aria-live="assertive" aria-atomic="true">
            <div class="d-flex">
                <div class="toast-body" id="toastMessage">
                    提示信息
                </div>
                <button type="button" class="btn-close me-2 m-auto" data-bs-dismiss="toast" aria-label="Close"></button>
            </div>
        </div>
    </div>
    
    <script src="/api/assets/js/bootstrap.bundle.min.js"></script>
    <script>
        // 页面加载时从后端获取应用数据
        document.addEventListener('DOMContentLoaded', function() {
            // 从后端API获取应用列表
            fetch('/api/getAppList')
                .then(response => response.json())
                .then(data => {
                    const appSelect = document.getElementById('appSelect');
                    
                    // 填充应用下拉框
                    data.forEach(app => {
                        const option = document.createElement('option');
                        option.value = app.id;
                        option.textContent = app.name;
                        appSelect.appendChild(option);
                    });
                })
                .catch(error => {
                    console.error('加载应用数据失败:', error);
                    showToast('加载应用数据失败，请刷新页面重试', 'danger');
                });
            
            // 监听应用选择变化，显示完整app名称
            document.getElementById('appSelect').addEventListener('change', function() {
                const selectedOption = this.options[this.selectedIndex];
                const appNameDisplay = document.getElementById('appNameDisplay');
                if (selectedOption.value) {
                    appNameDisplay.textContent = '当前选择: ' + selectedOption.textContent;
                    appNameDisplay.style.display = 'block';
                } else {
                    appNameDisplay.style.display = 'none';
                }
            });
            
            // 切换查询方式
            document.getElementById('searchType').addEventListener('change', function() {
                const kuaishouIdField = document.getElementById('kuaishouIdField');
                const phoneNumberField = document.getElementById('phoneNumberField');
                
                if (this.value === 'kuaishouId') {
                    kuaishouIdField.style.display = 'block';
                    phoneNumberField.style.display = 'none';
                    document.getElementById('phoneNumber').value = '';
                } else {
                    kuaishouIdField.style.display = 'none';
                    phoneNumberField.style.display = 'block';
                    document.getElementById('kuaishouId').value = '';
                }
            });
            
            // 添加悬浮按钮点击事件 - 跳转到事件查询页面
            document.getElementById('eventSearchButton').addEventListener('click', function() {
                window.location.href = '/api/event-search.html';
            });
            
            // 提交表单直接推送数据
            document.getElementById('pushForm').addEventListener('submit', function(e) {
                e.preventDefault();
                
                const appId = document.getElementById('appSelect').value;
                const searchType = document.getElementById('searchType').value;
                
                // 根据选择的查询方式获取值
                let value = '';
                if (searchType === 'kuaishouId') {
                    value = document.getElementById('kuaishouId').value;
                } else {
                    value = document.getElementById('phoneNumber').value;
                }
                
                // 验证输入
                if (appId === '') {
                    showToast('请选择应用', 'warning');
                    return;
                }
                
                if (value === '') {
                    showToast(searchType === 'kuaishouId' ? '请输入快手ID' : '请输入手机号', 'warning');
                    return;
                }
                
                // 显示加载中
                document.getElementById('loading').style.display = 'block';
                
                // 准备推送数据，只发送必要信息给后端
                const pushData = {
                    appId: appId,
                    searchType: searchType,
                    value: value
                };
                
                // 发送推送请求
                fetch('/api/pushDataToKuaishou', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(pushData)
                })
                .then(response => response.json())
                .then(data => {
                    document.getElementById('loading').style.display = 'none';
                    
                    // 显示推送结果
                    if (data.code === 0) {
                        showToast('推送成功', 'success');
                    } else {
                        showToast('推送失败: ' + data.message, 'danger');
                    }
                })
                .catch(error => {
                    console.error('推送失败:', error);
                    document.getElementById('loading').style.display = 'none';
                    showToast('推送失败: ' + error.message, 'danger');
                });
            });
        });
        
        // 显示提示弹窗
        function showToast(message, type) {
            const toastEl = document.getElementById('resultToast');
            const toastMessage = document.getElementById('toastMessage');
            
            // 设置消息内容
            toastMessage.textContent = message;
            
            // 移除以前的颜色类
            toastEl.classList.remove('bg-success', 'bg-danger', 'bg-warning', 'text-white');
            
            // 根据类型设置颜色
            if (type === 'success') {
                toastEl.classList.add('bg-success', 'text-white');
            } else if (type === 'danger') {
                toastEl.classList.add('bg-danger', 'text-white');
            } else if (type === 'warning') {
                toastEl.classList.add('bg-warning');
            }
            
            // 创建Bootstrap Toast实例
            const toast = new bootstrap.Toast(toastEl, {
                delay: 1000 // 1秒后自动消失
            });
            
            // 显示弹窗
            toast.show();
        }
    </script>
</body>
</html> 